package com.mop.sg2.map;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.executor.OrderedThreadPoolExecutor;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.logging.MdcInjectionFilter;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class AndroidServer {

	public static void main(String[] args) {
		AndroidServer server = new AndroidServer();
		server.start();
	}

	NioSocketAcceptor acceptor = null;

	private ExecutorService filterExecutor = new OrderedThreadPoolExecutor();// apache
																				// 提供的线程池，有参数可以设置，但不知道设置什么时候比较好，就用了默认的，从参数的设置上看，（因为没看源码，我自己猜测）其应该是使用的jdk的线程池

	public synchronized void start() {
		acceptor = new NioSocketAcceptor(Runtime.getRuntime()
				.availableProcessors());

		acceptor.setReuseAddress(true);
		acceptor.getSessionConfig().setReadBufferSize(2048);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		// Decrease the default receiver buffer size
		((SocketSessionConfig) acceptor.getSessionConfig())
				.setReceiveBufferSize(512);

		MdcInjectionFilter mdcFilter = new MdcInjectionFilter();

		acceptor.getFilterChain().addLast("mdcFilter", mdcFilter);
		acceptor.getFilterChain().addLast("threadPool",
				new ExecutorFilter(filterExecutor));// 设置线程池，以支持多线程
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new GameCodecFactory()));// 协议
		acceptor.getFilterChain().addLast("mdcFilter2", mdcFilter);
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		acceptor.setHandler(new MessageHandle());// 你自己的处理类
		try {
			acceptor.bind((new InetSocketAddress("10.2.23.58", 8089)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
